package players;
import evaluator.ElectricalResistanceFinder;
import game.Cell;
import game.HexBoard;
import game.Location;

import java.awt.Color;
import java.util.ArrayList;

import Hsearch.HSearch;

public class MyPlayerHSearch extends Player{
	public MyPlayerHSearch(Color color) {
		super(color);
	}
	
	@Override
	public Cell itsYourTurn(HexBoard hexBoard) { 
		int depth = 2;
//		Location loc = alphaBetaSearchMin(hexBoard, depth);
		return new Cell(0, 0, this);			
	}
	
	//getPossibleMoves(): build list of ALL empty cells
	private ArrayList<Location> getPossibleMoves(HexBoard hexBoard)
	{
		ArrayList<Location> moveList = new ArrayList<Location>();
		for(int i = 0; i < hexBoard.getSize(); i++)
		{
			for(int j = 0; j < hexBoard.getSize(); j++)
			{
				Cell aCell = hexBoard.getCell(i, j);
				if(aCell.getPlayer().getName().equals("Player 3"))
				{
					moveList.add(new Location(i,j));
				}
			}
		}
		return moveList;
	}
	
//	///alphaBetaSearchMax(): Select BestMove for Player1
//	private Location alphaBetaSearchMax(HexBoard hexBoard, int depth) {
//		HexBoard hexBoardClone = hexBoard.clone();	//copy the original board
//		MoveEvaluation bestMove = getMinValue(hexBoardClone, this, depth, Integer.MIN_VALUE, Integer.MAX_VALUE);
//		
//		//Print move
//		System.out.println(this.getName() + ", BEST MOVE: (" + bestMove.getLocation().getX() + "," + bestMove.getLocation().getY() + "), Score:" + bestMove.getScore());
//		return bestMove.getLocation();
//	}	
	
//	///alphaBetaSearchMin(): Select BestMove for Player2
//	private Location alphaBetaSearchMin(HexBoard hexBoard, int depth) {
//		HexBoard hexBoardClone = hexBoard.clone();	//copy the original board		
//		MoveEvaluation bestMove = getMaxValue(hexBoardClone, this, depth, Integer.MIN_VALUE, Integer.MAX_VALUE);
//
//		//Print move
//		System.out.println(this.getName() + ", BEST MOVE: (" + bestMove.getLocation().getX() + "," + bestMove.getLocation().getY() + "), Score:" + bestMove.getScore());
//		return bestMove.getLocation();
//	}		
	
//	//getMinValue(): Find the Smallest Value
//	private MoveEvaluation getMinValue(HexBoard hexBoard, Player player, 
//			int depth, int alpha, int beta) {
//		System.out.println("getMinValue: depth=" + depth +  ", alpha:" + alpha + ", beta: " + beta + ", player=" + player.getName());		
//		
//	    if (depth <= 0 || terminalTest(hexBoard))
//	    {
//	        int value = computeUtility(hexBoard);	//evaluate state	        
//	        return new MoveEvaluation(value, null);
//	    }
//      //find all possible moves for the children
//			ArrayList<Location> childrenMoveList = getPossibleMoves(hexBoard);
//			
//			Location firstMove = childrenMoveList.get(0);
//			MoveEvaluation best = new MoveEvaluation(Integer.MAX_VALUE, firstMove); //start with Highest value
//			
//			for ( int i = 0; i < childrenMoveList.size() && alpha < beta; ++i ) { 
//				//player makes move here
//				HexBoard hexBoardClone = hexBoard.clone();	//copy the original board
//				Location move = childrenMoveList.get(i);
//				Cell cell = new Cell(move.getX(), move.getY(), player);
//				//System.out.println( player.getName() + " makes a Virtual Move: (" + cell.getX() + "," + cell.getY() +")");
//				hexBoardClone.setMove(cell);	
//
//				//swap player, find the max value for the children
//				MoveEvaluation value = getMaxValue(hexBoardClone, hexBoardClone.getOtherPlayer(player), depth-1, alpha, beta);
//
//				//keep the LOWEST of the MAX
//				if ( value.getScore() < best.getScore() ) 
//				{
//					best = new MoveEvaluation(value.getScore(), move);
//					if ( value.getScore() < beta )
//						beta = value.getScore();
//				}
//			}
//			return best;
//	}
	
//	///getMaxValue(): Find the largest value
//	private MoveEvaluation getMaxValue(HexBoard hexBoard, Player player, 
//			int depth, int alpha, int beta) {
//		System.out.println("getMaxValue: depth=" + depth +  ", alpha:" + alpha + ", beta: " + beta + ", player=" + player.getName());	
//		
//	    if (depth <= 0 || terminalTest(hexBoard))
//	    {
//	        int value = computeUtility(hexBoard);	//evaluate state
//	        return new MoveEvaluation(value, null);
//	    }
//      //find all possible moves for the children
//			ArrayList<Location> childrenMoveList = getPossibleMoves(hexBoard);
//			
//			Location firstMove = childrenMoveList.get(0);
//			MoveEvaluation best = new MoveEvaluation(Integer.MIN_VALUE, firstMove); //start with lowest value			
//			
//			for ( int i = 0; i < childrenMoveList.size() && alpha < beta; ++i ) {
//				//player makes move here
//				HexBoard hexBoardClone = hexBoard.clone();	//copy the original board
//				Location move = childrenMoveList.get(i);
//				Cell cell = new Cell(move.getX(), move.getY(), player);
//				//System.out.println( player.getName() + " makes a Virtual Move: (" + cell.getX() + "," + cell.getY() +")");
//				hexBoardClone.setMove(cell);
//
//				//swap player, find the MIN value for the children
//				MoveEvaluation value = getMinValue(hexBoardClone, hexBoardClone.getOtherPlayer(player), depth-1, alpha, beta);
//				
//				//keep the Highest of the MINs
//				if ( value.getScore() > best.getScore() ) 
//				{
//					best = new MoveEvaluation(value.getScore(), move);
//					if ( value.getScore() > alpha )
//						alpha = value.getScore();
//				}
//			}
//			return best;
//	}
	
	//terminalTest(): Check if it's game over
	public boolean terminalTest(HexBoard hexBoard) {
		Player winner = hexBoard.getWinner(); 
		return (winner != null) ? true: false;	//winner found
	}	
	
	///computeUtility: Calculate the cost for the move
	private int computeUtility(HexBoard hexBoard) {
		Player winner = hexBoard.getWinner();
	    String playerToMove = hexBoard.getLastMove().getPlayer().getName();	
	    
		if (winner != null) { //1. Is Game Over?
			if (playerToMove.equals(this.getName())) {
				return Integer.MAX_VALUE;
			}
      return Integer.MIN_VALUE;
		}
    return estimate(hexBoard);
	}	
	
	//estimate(): Check which player is winning so far
	private int estimate(HexBoard hexBoard)
	{
		//1. Call H-Search
		HSearch hSearch = new HSearch();
		HexBoard hexBoard2 = hSearch.SearchConnections(hexBoard.clone());				
		//hexBoard2.Display();
		
		//2. Calculate Resistance 
		Player player = hexBoard.getLastMove().getPlayer();
		ElectricalResistanceFinder e = new ElectricalResistanceFinder(hexBoard2, player) ;
		return (int)e.getEqResistance() ;			
	}

	
//	//getNumberOfItems(): number of filled cells
//	private int getNumberOfItems(HexBoard hexBoard)
//	{
//		int qty = 0;
//		for(int i = 0; i < hexBoard.getSize(); i++)
//		{
//			for(int j = 0; j < hexBoard.getSize(); j++)
//			{
//				Cell aCell = hexBoard.getCell(i, j);
//				if(!aCell.getPlayer().getName().equals("Player 3"))
//				{
//				   qty++;
//				}
//			}
//		}
//		return qty;
//	}	
}
